<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*
            思路：找出数组中最大的那个节点，它就作为根节点。
            然后数组左边就是左子树，右边是右子树。
            然后再找出数组左边的最大值，作为左子树根节点，以此递归
            */
            var constructMaximumBinaryTree = function (nums) {
                const buildTree = (arr) => {
                    if (!arr.length) return null
                    let maxIndex = -1
                    let maxValue = -1
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i] > maxValue) {
                            maxValue = arr[i]
                            maxIndex = i
                        }
                    }
                    let root = new TreeNode(maxValue)
                    root.left = buildTree(arr.slice(0, maxIndex))
                    root.right = buildTree(arr.slice(maxIndex + 1))
                    return root
                }
                return buildTree(nums)
            }
        </script>
    </body>
</html>
